<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Request;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Validator;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout', 'redirectToGithubAuthPage', 'toutiaoCallback', 'toutiaoAuth']]);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    public function AuthWeibo(Request $request)
    {
        $str = implode('======', $request->all());
        $actor = new Actor();
        $actor->name_en = $str;
        $actor->save();
        echo $actor;
    }

    public function AuthWeixin(Request $request)
    {
        $str = implode('======', $request->all());
        $actor = new Actor();
        $actor->name_en = $str;
        $actor->save();
        echo $actor;
    }

    public function AuthQQ(Request $request)
    {
        $str = implode('======', $request->all());
        $actor = new Actor();
        $actor->name_en = $str;
        $actor->save();
        echo $actor;
    }

    public function toutiaoAuth()
    {
        $callback = urlencode('http://www.trytv.org/auth/toutiao-callback');
        $toutiao_auth_url = "https://open.snssdk.com/auth/authorize?response_type=code&auth_only=1&client_key=b6c1c1b63946497b&redirect_uri=$callback";
        return redirect($toutiao_auth_url);
    }

    public function toutiaoCallback()
    {
        $code = $_GET['code'];
        $url = "https://open.snssdk.com/auth/token?grant_type=authorize_code&code=$code&client_key=b6c1c1b63946497b&client_secret=8b99dedcf5f29345f7d2b22de8636050";
        $json = file_get_contents($url);
        $json = \GuzzleHttp\json_decode($json);
        //dd($json); 有效期两个月

        // 存储数据到session...
        session('toutiao_token', $json->data->access_token);
        $_COOKIE['toutiao_token'] = $json->data->access_token;
        dd($json->data);
        return redirect('/article');
    }


}
